<template>
  <header class="header-box">
    <div class="text">
      <div class="time">
        <span>{{time.day}}</span>
        <span>{{ time.month }}</span>
      </div>
      <h2 class="title">知乎日报</h2>
    </div>
    <div class="picture">
      <router-link to="person">
        <img class="logo" :src="pic" alt="" />
      </router-link>
    </div>
  </header>
</template>

<script>
import {formatTime} from '@/assets/utils'
import { useStore } from 'vuex';
import { computed, onBeforeMount } from '@vue/runtime-core';
import timg from "@/assets/images/timg.jpg"
export default {
  name: "HomeHeader",
  props: {
    today: {
      type: String,
      required: true,
    },
  },
  setup(props){

    let  store = useStore()//仓库

    // 头像处理
    let pic = computed(()=>{
      let {info} = store.state
      return info ? info.pic : timg
    })
    // 第一次渲染时,验证你当前是否登录
    // 验证方式:
    //    1)通过localstorage中是否有token来验证
    //    2)vuex中如果isLogin是true表示已登录
    onBeforeMount(()=>{
      let {isLogin} = store.state
      // null表示不知道是否登录
      // 即使你登陆了,但是当你刷新时,vuex中的数据也会变为null
      if(isLogin === null){
        store.dispatch('changeIsLoginAsync')
      }
    })

    // 计算日期
    let  time = computed(()=>{
      let today = props.today || null,
      arr = [
         "",
          "一月",
          "二月",
          "三月",
          "四月",
          "五月",
          "六月",
          "七月",
          "八月",
          "九月",
          "十月",
          "十一月",
          "十二月",
      ]
      let [month, day] = formatTime(today, "{1}-{2}").split("-");
      return {
        day,
        month: arr[+month],
      };
    })
    return {
      time,
      pic
    }
  }
};
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  .picture {
    width: 34px;
    height: 34px;
    box-sizing: border-box;
    border-radius: 50%;
    overflow: hidden;
    a {
      display: block;
      width: 100%;
      height: 100%;
      .logo {
        display: block;
        width: 100%;
        height: 100%;
      }
    }
  }
  .text{
      display: flex;
      align-items: center;
      height: 34px;
      justify-content: flex-start;
      .title{
          font-size: 24px;
          border-left: 1px solid ;
          padding-left: 10px;
      }
      .time{
          box-sizing: border-box;
          padding-right: 15px;
          span{
              display: block;
              font-size: 20px;
              text-align: center;
              line-height: 20px;
              &:nth-last-child(1){
                  font-size: 12px;
                  line-height: 14px;
              }
          }
      }
  }
}
</style>